
var ViewModel = function () {
    var me = this;
    me.$tabs = null;
    me.data = null;
    me.map = {};
    me.$frame = null;
    me.ftitle=null;
    me.list=null;

    me.init = function () {
        me.$tabs = $('#main_tabs');
        me.initMenu();
        me.initUserMenu();
    }; // -- init

    /**
     * param:data,表示菜单数据
     * param:current_class,表示当前元素的类名
     * param:parent_node,表示上级元素的id,
     * param:current_id,表示当前元素的id;
     *
     */
    me.initUserMenu=function () {
        $('#btn_logout').click(me.logout);
        $('#btn-changePassword').click(me.changePassword);
    }
    me.logout=function () {
        layer.confirm('您确定要退出登录？',{title:'登出提醒'},function (index) {
            location.href =logout_url;
        })
    };
    me.changePassword=function () {
        layer_open('修改密码',changePassword_url)
    }
    
    
    me.initMenu = function () {
        // $.getJSON(get_menu_url, function (data) {
        //     var className = "sidebar-menu";
        //     var $menu = $('#side_section');
        //     var name = 'side_bar';
        //
        //     me.createMenu(data, className, $menu, name);
        //     me.showHomepage();
        // });

        var className = "sidebar-menu";
        var $menu = $('#side_section');
        var name = 'side_bar';

        me.createMenu(admin_menu_data, className, $menu, name);
        me.showHomepage();
    };

    me.createMenu = function (data, className, $menu, name) {
        var $ul;
        var $li;
        $ul = '<ul id=' + name + ' class="' + className + '"></ul>';

        $($ul).appendTo($menu);
        for (var i = 0; i < data.length; i++) {
            var arr = data[i];
            if (!arr.children) {
                arr.children = [];
            }

            if (arr.children && arr.children.length > 0) {
                $li = '<li id=' + arr.id + ' class="treeview"> <a href="#"><i class="' + arr.icon + '"></i> <span>' + arr.text + '</span> <i class="fa fa-angle-left pull-right"></i></a></li>';
            } else {
                $li = '<li id="' + arr.name + '" frame_url="'+arr.frame_url+'"><a href="javascript:void(0)" style="cursor: auto"><i class="' + arr.icon + '"></i>  <span>' + arr.text + '</span> </a></li>';
            }
            $($li).appendTo($('#' + name));
            if (arr.children.length != 0) {
                me.createMenu(arr.children, 'treeview-menu', $("#" + arr.id), arr.name);
            } else {
                $('#' + arr.name).click(function (e) {
                    var url=$(this).attr('frame_url');
                    me.showFrame(e.currentTarget.id,url,e.currentTarget.innerText);
                }
                );
            }

        }
    }; // -- createMenu

    me.showHomepage = function () {
        addTabs({id:'home', title: '首页' ,close: false,url:  url_build(int_url)});
    }; // -- showHomepage

    me.showFrame = function (id,url,title) {

        addTabs({id:id, title: title ,close: true,url:url});
    };


}; // -- ViewModel

var vm=new ViewModel();
vm.init();